// /////////////////////////////////////////////////////////////////////////////
//
// Name:            LuaFrame.cpp
// Author:          Michael Bartsch
//
// Desc :           This file contains the CLuaFrame Class. This class
//                  contains the Lua Prompt which can be used to interact
//                  with the Lua scripting language.
//
// License:         Copyright (C) 2008-2009 Michael Bartsch
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the GNU General Public
//                  License as published by the Free Software Foundation,
//                  either version 3 of the License, or (at your option) any
//                  later version.
//
//                  This program is distributed in the hope that it will be
//                  useful, but WITHOUT ANY WARRANTY; without even the implied
//                  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//                  PURPOSE. See the GNU General Public License for more
//                  details.
//
//                  You should have received a copy of the GNU General Public
//                  License along with this program. If not, see
//                  <http://www.gnu.org/licenses/>.
//
// /////////////////////////////////////////////////////////////////////////////

// Include required header file(s).
#include "LuaFrame.h"


// CLuaFrame Events. - [ BEGIN ]
BEGIN_EVENT_TABLE(CLuaFrame, wxFrame)
   EVT_CLOSE(CLuaFrame::onClose)

   EVT_MENU(wxID_OPEN,                      CLuaFrame::onOpen)
   EVT_MENU(wxID_EXIT,                      CLuaFrame::onQuit)

   EVT_MENU(LF_Menu_ResetCommands,          CLuaFrame::onResetCommands)
   EVT_MENU(LF_Menu_ClearTerminal,          CLuaFrame::onClearTerminal)
   EVT_MENU(LF_Menu_ResetTerminal,          CLuaFrame::onResetTerminal)
   EVT_MENU(LF_Menu_ResetClearTerminal,     CLuaFrame::onResetClearTerminal)

   EVT_TEXT_ENTER(LF_InputControl, CLuaFrame::onSubmitInput)
END_EVENT_TABLE()
// CLuaFrame Events. - [ END ]


// CLuaFrame.
// Constructor.
CLuaFrame::CLuaFrame(wxWindow *parent)
: wxFrame(parent, wxID_ANY, _T("Aia|CP - Lua Terminal"), wxPoint(0,0), wxSize(800,600), wxDEFAULT_FRAME_STYLE & ~ (wxRESIZE_BORDER | wxRESIZE_BOX | wxMAXIMIZE_BOX))
{
    // Set internal variables.
    frameParent = (CMainFrame*)parent;

    // Initialise Lua.
    pScriptManager = CScriptManager::Instance();
    pScriptManager->clear();
    pScriptManager->init();

    // Init the Menu Bar.
    wxMenuBar* menuBar = new wxMenuBar();

    // Menu->File Menu
    wxMenu *fileMenu = new wxMenu;

    wxMenuItem *fmOpen = new wxMenuItem(fileMenu, wxID_OPEN, _T("&Open...\tCtrl+O"), _T("Open a file"));
    fmOpen->SetBitmap(wxIcon("../media/file-open.png", wxBITMAP_TYPE_PNG));

    wxMenuItem *fmQuit = new wxMenuItem(fileMenu, wxID_EXIT, _T("&Quit\tCtrl+Q"), _T("Close the Lua Terminal"));
    fmQuit->SetBitmap(wxIcon("../media/file-quit.png", wxBITMAP_TYPE_PNG));

    fileMenu->Append(fmOpen);
    fileMenu->AppendSeparator();
    fileMenu->Append(fmQuit);

    // Menu->Terminal Menu
    wxMenu *terminalMenu = new wxMenu;

    terminalMenu->Append(LF_Menu_ResetCommands, _T("&Reset Commands"), _T("Reset the command box"));
    terminalMenu->AppendSeparator();
    terminalMenu->Append(LF_Menu_ClearTerminal, _T("&Clear"), _T("Clear the terminal"));
    terminalMenu->Append(LF_Menu_ResetTerminal, _T("&Reset"), _T("Reset the terminal"));
    terminalMenu->Append(LF_Menu_ResetClearTerminal, _T("Reset and Clear"), _T("Reset the terminal and clear it"));

    // Add submenu's to the Menu Bar.
    menuBar->Append(fileMenu, _T("&File"));
    menuBar->Append(terminalMenu, _T("&Terminal"));

    // Associate the Menu Bar with the frame.
    SetMenuBar(menuBar);

    // Init the Status Bar.
    CreateStatusBar(1, 0, -1);

    // Create the prompt.
    wxPanel *mainPanel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(800,600));
    wxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);

    // Input box.
    inputControl = new wxComboBox(mainPanel, LF_InputControl, _T(""), wxDefaultPosition, wxDefaultSize, 0, NULL, wxCB_DROPDOWN | wxTE_PROCESS_ENTER);
    mainSizer->Add(inputControl, wxSizerFlags().Expand().Border(wxTOP, 2));

    // Output box.
    outputControl = new wxTextCtrl(mainPanel, LF_OutputControl, _T("Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio\n> "), wxDefaultPosition, wxSize(0, 510), wxTE_MULTILINE | wxTE_READONLY | wxALWAYS_SHOW_SB | wxVSCROLL);
    mainSizer->Add(outputControl, wxSizerFlags().Expand().Border(wxTOP, 2));

    wxFont *font = new wxFont(9, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
	outputControl->SetFont(*font);
	delete font;

    // Set sizer.
    mainPanel->SetSizer(mainSizer);
    mainSizer->SetSizeHints(mainPanel);
}

// Event Functions. - [ BEGIN ]
void CLuaFrame::onClose(wxCloseEvent &event)
{
    // Reset frame state.
    frameParent->resetLuaFrame();

    // Destroy frame.
    Destroy();
}

void CLuaFrame::onOpen(wxCommandEvent& event)
{
    wxFileDialog *fileDialog = new wxFileDialog(this, "Open...", ".", "", "Lua files (*.lua)|*.lua", wxFD_OPEN, wxDefaultPosition, wxDefaultSize);

    if(fileDialog->ShowModal() == wxID_OK)
    {
       pScriptManager->executeFile(fileDialog->GetPath());

       outputControl->AppendText(_T("Loading file: "));
       outputControl->AppendText(fileDialog->GetPath());
       outputControl->AppendText(_T("\n"));
       outputControl->AppendText(pScriptManager->getOutput());
       outputControl->AppendText(_T("> "));
    }

    else return;
}

void CLuaFrame::onQuit(wxCommandEvent& event)
{
    Close(true);
}

void CLuaFrame::onResetCommands(wxCommandEvent& event)
{
    inputControl->Clear();
}

void CLuaFrame::onClearTerminal(wxCommandEvent& event)
{
    outputControl->SetValue(_T("Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio\n> "));
}

void CLuaFrame::onResetTerminal(wxCommandEvent& event)
{
    pScriptManager->clear();
    pScriptManager->init();
}

void CLuaFrame::onResetClearTerminal(wxCommandEvent& event)
{
    // Reset
    pScriptManager->clear();
    pScriptManager->init();

    // Clear
    outputControl->SetValue(_T("Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio\n> "));
}

void CLuaFrame::onSubmitInput(wxCommandEvent &event)
{
    if(event.GetString() != _T(""))
    {
       // Execute command.
       pScriptManager->executeString(event.GetString());

       // Modify the Output Control.
       outputControl->AppendText(event.GetString());
       outputControl->AppendText(_T("\n"));
       outputControl->AppendText(pScriptManager->getOutput());
       outputControl->AppendText(_T("> "));

       // Modify the Input Control.
       inputControl->Append(event.GetString());
       inputControl->SetValue(_T(""));
    }
}
// Event Functions. - [ END ]

// End of File
